#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int mdsend_sms(int fd, char *tel, char *text){
  
  int len;
  int result;
  char *received;
  int tel_len = 0;
  int text_len = 0;
  char *response;
  char *send_sms_command;
  char *pom;

  
  if ((tel == NULL) || ((tel_len=strlen(tel))>20))
  {
    fprintf(stderr, "telefonne cislo je NULL alebo ma viac ako 20 znakov\n");
    return 1;
  }
  if (text == NULL)
  {
    text = malloc(sizeof(char));
    text[0] = '\0';
  }
  if ((text_len = strlen(text)) > 160)
  {
    fprintf(stderr, "text sms ma viac ako 160 znakov\n");
    return 1;
  }
  
  send_sms_command = (char *) malloc((text_len + tel_len + 20)*sizeof(char));
  sprintf(send_sms_command, "RESUME\nSENDSMS %s %s\n", tel, text);
  //printf("send=|%s|\n", send_sms_command);	
  if (mdsend_command_and_get_nresponses(fd, send_sms_command, &response, 2) == -1)
  {
    return 1;
  }
  else if (((pom = strstr(response, "200")) != NULL) && (strlen(pom)>2) && (strstr((pom+1), "200") != NULL))
  {
    return 0;
  }
  else
  {
    fprintf(stderr, "sms sa nepodarilo odoslat: %s\n", response);
    return 1;
  }
    
}

